package org.liziyi.pss.gui;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

/**
 * @author liziyi
 */
public class QueueGroupGUI {
    public JPanel baseJPanel;
    public JTable readyQueueJTable;
    public JTable backupQueueJTable;
    public JTable suspendQueueJTable;
    public JTable finishQueueJTable;

    public void createUIComponents() {
        readyQueueJTable = new JTable(new DefaultTableModel(new Object[]{"PID", "时间片", "优先权", "内存", "状态", "属性"}, 0)) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        backupQueueJTable = new JTable(new DefaultTableModel(new Object[]{"PID", "时间片", "优先权", "内存", "状态", "属性"}, 0)) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        suspendQueueJTable = new JTable(new DefaultTableModel(new Object[]{"PID", "时间片", "优先权", "内存", "状态", "属性"}, 0)) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        finishQueueJTable = new JTable(new DefaultTableModel(new Object[]{"pid", "属性"}, 0)) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        // 设置文字居中
        DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer(){
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                c.setBackground(Color.WHITE);
                if(value.toString().startsWith("同步")){
                    c.setForeground(Color.blue);
                } else{
                    c.setForeground(Color.black);
                }
                return c;
            }
        };
        defaultTableCellRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        readyQueueJTable.setDefaultRenderer(Object.class, defaultTableCellRenderer);
        backupQueueJTable.setDefaultRenderer(Object.class, defaultTableCellRenderer);
        suspendQueueJTable.setDefaultRenderer(Object.class, defaultTableCellRenderer);
        finishQueueJTable.setDefaultRenderer(Object.class, defaultTableCellRenderer);
        readyQueueJTable.setEnabled(false);
        backupQueueJTable.setEnabled(false);
        suspendQueueJTable.setEnabled(false);
        finishQueueJTable.setEnabled(false);
        // 设置列宽度比例
        readyQueueJTable.getColumnModel().getColumn(5).setPreferredWidth((int) (readyQueueJTable.getPreferredSize().width * 0.3));
        backupQueueJTable.getColumnModel().getColumn(5).setPreferredWidth((int) (backupQueueJTable.getPreferredSize().width * 0.3));
        suspendQueueJTable.getColumnModel().getColumn(5).setPreferredWidth((int) (suspendQueueJTable.getPreferredSize().width * 0.3));

    }

    public JTable getReadyQueueJTable() {
        return readyQueueJTable;
    }

    public JTable getBackupQueueJTable() {
        return backupQueueJTable;
    }

    public JTable getSuspendQueueJTable() {
        return suspendQueueJTable;
    }

    public JTable getFinishQueueJTable() {
        return finishQueueJTable;
    }
}
